package com.by.yz.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.NetworkImageView;
import com.by.yz.App;
import com.by.yz.R;
import com.by.yz.activity.base.BaseActivity;
import com.by.yz.chatuidemo.Constant;
import com.by.yz.chatuidemo.DemoHelper;
import com.by.yz.chatuidemo.ui.ChatActivity;
import com.by.yz.custom.CircleNetworkImageView;
import com.by.yz.entity.Comment;
import com.by.yz.entity.TopicInfo;
import com.by.yz.entity.UserTest;
import com.by.yz.loginUtil.UserRESTFul;
import com.by.yz.util.ToJSonObject;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.widget.EaseAlertDialog;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by 你只能呵呵 on 2016/10/15.
 */

public class AddFriendActivity extends BaseActivity {

    private CircleNetworkImageView networkImageView;//个人头像
    private ProgressDialog progressDialog;
    UserTest userTest;
    TextView nickname;
    TextView login;
    TextView sex;
    Button Add,Chat;
    String topicer,commenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_friend);
        setToolbarTitle("查看资料");
        initView();
    }

    private void initView(){
        nickname=(TextView)findViewById(R.id.pc_my_setting_text1);
        login=(TextView)findViewById(R.id.personalNum);
        sex=(TextView)findViewById(R.id.personalSex) ;
        networkImageView = (CircleNetworkImageView)findViewById(R.id.headerPicture1);
        networkImageView.setDefaultImageResId(R.mipmap.login_logo1);
        networkImageView.setErrorImageResId(R.mipmap.login_error);
        try {
            topicer=getIntent().getStringExtra("currentopics");
            Log.e("ylj","添加朋友"+topicer);
            login.setText(topicer);
            UserRESTFul userRESTFul=new UserRESTFul();
            userRESTFul.getUser(topicer, new UserRESTFul.UserPOSTCallBack() {
                @Override
                public void ok(JSONObject response) throws JSONException {
                    userTest= ToJSonObject.jsonToBean(response.toString(),UserTest.class);
                    nickname.setText(userTest.getnickName());
                    login.setText(userTest.getPhonenumber());
                    //个人头像
                    if (userTest.getHeadimg()!=null){
                        networkImageView.setImageUrl(userTest.getHeadimg(),App.imageLoader);
                    }
                    if(userTest.getsex()==true) {
                        sex.setText("男");
                    }
                    else{
                        sex.setText("女");
                    }
                }

                @Override
                public void error(String response) {
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
        try{
            commenter=getIntent().getStringExtra("currentcomment");
            Log.e("ylj","评论头像"+commenter);
            login.setText(commenter);
            UserRESTFul userRESTFul=new UserRESTFul();
            userRESTFul.getUser(commenter, new UserRESTFul.UserPOSTCallBack() {
                @Override
                public void ok(JSONObject response) throws JSONException {
                    userTest= ToJSonObject.jsonToBean(response.toString(),UserTest.class);
                    nickname.setText(userTest.getnickName());
                    login.setText(userTest.getPhonenumber());
                    //个人头像
                    if(userTest.getHeadimg()!=null){
                        networkImageView.setImageUrl(userTest.getHeadimg(),App.imageLoader);
                    }
                    if(userTest.getsex()==true) {
                        sex.setText("男");
                    }
                    else{
                        sex.setText("女");
                    }
                }

                @Override
                public void error(String response) {
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
        Add=(Button)findViewById(R.id.btn_add_friend);
        Add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(EMClient.getInstance().getCurrentUser().equals(login.getText().toString())){
                    new EaseAlertDialog(AddFriendActivity.this, R.string.not_add_myself).show();
                    return;
                }

                if(DemoHelper.getInstance().getContactList().containsKey(login.getText().toString())){
                    //let the user know the contact already in your contact list
                    if(EMClient.getInstance().contactManager().getBlackListUsernames().contains(login.getText().toString())){
                        new EaseAlertDialog(AddFriendActivity.this, R.string.user_already_in_contactlist).show();
                        return;
                    }
                    new EaseAlertDialog(AddFriendActivity.this, R.string.This_user_is_already_your_friend).show();
                    return;
                }

                progressDialog = new ProgressDialog(AddFriendActivity.this);
                String stri = getResources().getString(R.string.Is_sending_a_request);
                progressDialog.setMessage(stri);
                progressDialog.setCanceledOnTouchOutside(false);
                progressDialog.show();

                new Thread(new Runnable() {
                    public void run() {

                        try {
                            //demo use a hardcode reason here, you need let user to input if you like
                            String s = getResources().getString(R.string.Add_a_friend);
                            EMClient.getInstance().contactManager().addContact(login.getText().toString(), s);
                            runOnUiThread(new Runnable() {
                                public void run() {
                                    progressDialog.dismiss();
                                    String s1 = getResources().getString(R.string.send_successful);
                                    Toast.makeText(getApplicationContext(), s1, Toast.LENGTH_LONG).show();
                                }
                            });
                        } catch (final Exception e) {
                            runOnUiThread(new Runnable() {
                                public void run() {
                                    progressDialog.dismiss();
                                    String s2 = getResources().getString(R.string.Request_add_buddy_failure);
                                    Toast.makeText(getApplicationContext(), s2 + e.getMessage(), Toast.LENGTH_LONG).show();
                                }
                            });
                        }
                    }
                }).start();
            }
        });
        Chat=(Button)findViewById(R.id.btn_send_xinxi);
        Chat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(EMClient.getInstance().getCurrentUser().equals(login.getText().toString())){
                    new EaseAlertDialog(AddFriendActivity.this, "不能和自己聊天").show();
                    return;
                }
                Intent intent=new Intent(AddFriendActivity.this,ChatActivity.class);
                intent.putExtra(Constant.EXTRA_USER_ID,topicer.toString());
                intent.putExtra("userName",nickname.getText().toString());
                startActivity(intent);
            }
        });

    }
}
